/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author Thibault
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "Article.getListArticleForUserId",
    query = "SELECT a "
    + "FROM Article a "
    + "WHERE a.seller.id = :id "
    + "ORDER BY a.endSale"),
    @NamedQuery(name = "Article.getListArticleForUserIdAndState",
    query = "SELECT a "
    + "FROM Article a "
    + "WHERE a.seller.id = :id "
    + "AND a.articleState = :articleState "
    + "ORDER BY a.endSale"),
    @NamedQuery(name = "Article.searchByName",
    query = "SELECT a "
    + "FROM Article a "
    + "WHERE UPPER(a.name) LIKE :keyWord "
    + "AND a.articleState = entities.ArticleState.forSale "
    + "ORDER BY a.endSale"),
    @NamedQuery(name = "Article.searchByNameAndCategory",
    query = "SELECT a "
    + "FROM Article a "
    + "WHERE UPPER(a.name) LIKE :keyWord "
    + "AND :category IN (a.categories) "
    + "AND a.articleState = entities.ArticleState.forSale "
    + "ORDER BY a.endSale")
})
public class Article implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String description;
    private double startPrice;
    @Temporal(TemporalType.TIMESTAMP)
    private Date endSale;
    @ManyToOne
    private UserProject seller;
    @ElementCollection(targetClass = Category.class)
    @JoinTable(name = "ARTICLE_CATEGORY", joinColumns =
    @JoinColumn(name = "id"))
    @Column(name = "category", nullable = false)
    @Enumerated(EnumType.STRING)
    private Collection<Category> categories = new ArrayList<Category>();
    @Enumerated(EnumType.STRING)
    private ArticleState articleState;
    @OneToMany(targetEntity = Bid.class, mappedBy = "article", cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    private List<Bid> biddingHistory = new ArrayList<Bid>();
    @Transient
    private Bid winningBid;
    @Transient
    private double actualPrice;

    public Article() {
    }

    public Article(String name, String description, double startPrice, Date endSale, UserProject seller) {
        this.name = name;
        this.description = description;
        this.startPrice = startPrice;
        this.endSale = endSale;
        this.seller = seller;
        this.articleState = ArticleState.forSale;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public UserProject getSeller() {
        return seller;
    }

    public void setSeller(UserProject seller) {
        this.seller = seller;
    }

    public double getStartPrice() {
        return startPrice;
    }

    public void setStartPrice(double startPrice) {
        this.startPrice = startPrice;
    }

    public double getActualPrice() {
        if (this.getWinningBid() != null) {
            actualPrice = this.getWinningBid().getPrice();
        } else {
            actualPrice = this.startPrice;
        }
        return actualPrice;
    }

    public void setActualPrice(double actualPrice) {
        this.actualPrice = actualPrice;
    }

    public Bid getWinningBid() {
        if (!biddingHistory.isEmpty()) {
            winningBid = biddingHistory.get(0);
            for (Bid b : biddingHistory) {
                if (b.getPrice() > winningBid.getPrice()) {
                    winningBid = b;
                }
            }
            return winningBid;
        }
        return null;
    }

    public void setWinningBid(Bid winningBid) {
        this.winningBid = winningBid;
    }

    public Date getEndSale() {
        return endSale;
    }

    public void setEndSale(Date endSale) {
        this.endSale = endSale;
    }

    public Collection<Category> getCategories() {
        return categories;
    }

    public void setCategories(Collection<Category> categories) {
        this.categories = categories;
    }

    public ArticleState getArticleState() {
        return articleState;
    }

    public void setArticleState(ArticleState articleState) {
        this.articleState = articleState;
    }

    public List<Bid> getBiddingHistory() {
        return biddingHistory;
    }

    public void setBiddingHistory(List<Bid> biddingHistory) {
        this.biddingHistory = biddingHistory;
    }
}
